
package com.x52im.mall.logic.score;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author LZ
 * 
 *         数据库相关操作的类
 */
public class DBAdapter
{
	/**
	 * 数据库名
	 */
	private static final String DATABASE_NAME = "rb_pro_mall.db";//"robotime_missu.db";

	/**
	 * 数据表名
	 */
	private static final String DATABASE_TABLE = "token_rechange_history";

	/**
	 * 数据库版本
	 */
	private static final int DATABASE_VERSION = 2;

	/**
	 * key_id ：主键
	 */
	public static final String KEY_ID = "key_id";

	/**
	 * content ：信息内容
	 */
	public static final String CONTENT = "content";

	/**
	 * forClassName : 类名
	 */
	public static final String CLS_NAME = "cls_name";
	
	/**
    * 
    */
	public static final String DATABASE_CREATE = "create table token_rechange_history (key_id INTEGER PRIMARY KEY," + "content TEXT," + "cls_name TEXT" + ");";

	/**
    * 
    */
	private final Context context;

	/**
    * 
    */
	private DatabaseHelper DBHelper;

	/**
    * 
    */
	private SQLiteDatabase db;

	public DBAdapter(Context ctx)
	{
		context = ctx;
		DBHelper = new DatabaseHelper(context);

	}

	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		public DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db)
		{
			// TODO Auto-generated method stub
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

	}

	/**
	 * 打开数据库
	 * 
	 * @return
	 * @throws SQLException
	 */
	public SQLiteDatabase open() throws SQLException
	{
		db = DBHelper.getWritableDatabase();

		Cursor cursor = getAll();

		if (cursor.getCount() == 0)
		{
//			Log.v("数据库为空", "数据库为空");// TODO
		}
		else
		{
			Log.v("数据库不为空", "读取数据");
		}
		cursor.close();

		return db;
	}

	/**
	 * 关闭数据库
	 */
	public void close()
	{
		DBHelper.close();
	}

	/**
	 * 向数据库中插入数据
	 */

	public long insert(String content, String clsName)
	{

		ContentValues initialValues = new ContentValues();
		initialValues.put(CONTENT, content);
		initialValues.put(CLS_NAME, clsName);

		return db.insert(DATABASE_TABLE, null, initialValues);
	}

	/**
	 * 删除数据
	 */
	public boolean delete(String key)
	{
		return db.delete(DATABASE_TABLE, KEY_ID + "='" + key + "'", null) > 0;
	}

	/**
	 * 删除数据
	 */
	public boolean deleteAll()
	{
		return db.delete(DATABASE_TABLE, "1=1", null) > 0;
	}

	public Cursor getAll()
	{
		Cursor cur = db.query(DATABASE_TABLE, null, null, null, null, null, null);
		return cur;
	}

	public Cursor get(String key) throws SQLException
	{
		String sql = "select * from " + DATABASE_TABLE + " where " + KEY_ID + " =?";
		Cursor cur = db.rawQuery(sql, new String[] { key });

		System.out.println("all : " + this.getAll().getCount() + ",get : " + cur.getCount());
		if (cur != null)
		{
			cur.moveToFirst();
		}
		return cur;
	}
}
